package model;

import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import util.HibernateUtil;

/**
 *
 * @author NEI
 */
public class GenericoDAO {
    
    public boolean  Salvar(Object obj){
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {           
            tx = s.beginTransaction();
            s.persist(obj);
            tx.commit();            
            return true;
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "Erro Salvar: "+e.getMessage());
            if (tx!=null){
                tx.rollback();
            }
            return false;
        } finally {
            s.close();
        }        
            
    }
    
    public boolean Excluir(Object obj){
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            s.delete(obj);
            tx.commit();
            return true;
        } catch (ConstraintViolationException e) {
            JOptionPane.showMessageDialog(null, "Erro ao excluir: O registro esta sendo referenciado em outro registro. ");
            if (tx!=null){
                tx.rollback();
            }
            return false;
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "Erro Excluir: "+e.getMessage());
            if (tx!=null){
                tx.rollback();
            }
            return false;
        } finally {
            s.close();
        }        
    }

    public boolean  Atualizar(Object obj){
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {            
            tx = s.beginTransaction();
            s.merge(obj);
            tx.commit();
            return true;
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "Erro Atualizar: "+e.getMessage());
            if (tx!=null){
                tx.rollback();
            }
            return false;
        }  finally {
            s.close();
        }       
    }
    
    public boolean SalvarOuAtualizar(Object obj){
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            s.saveOrUpdate(obj);
            tx.commit();  
            return true;
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "Erro SalvarOuAtualizar: "+e.getMessage());
            if (tx!=null){
                tx.rollback();
            }
            return false;
        } finally {
            s.close();
        }      
    }
    
    public List getAll(Class clazz){
        List lista = null;
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            lista = s.createQuery("from "+clazz.getName())
                     .list();
            tx.commit();
            return lista;            
        } catch (HibernateException e) {
            lista = null;
            JOptionPane.showMessageDialog(null, "Erro getAll: "+e.getMessage());
        } finally{
            s.close();
        }
        return lista;
    }
    
    public Object getById(Class clazz, Integer id){
        Object objGet = null;
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = s.beginTransaction();
            objGet = s.get(clazz, id);
            tx.commit();
        } catch (HibernateException e) { 
            objGet = null;
            JOptionPane.showMessageDialog(null, "Erro getById: "+e.getMessage());
        } finally {           
            s.close();
        }
        return objGet;
    }
    
    public void resetaBanco(){
        Session s = HibernateUtil.getSessionFactory().openSession();
        try {
            s.beginTransaction();            
            
            SQLQuery query = s.createSQLQuery("TRUNCATE abertura_caixa, " +
                    "cliente, " +
                    "comanda_cabecalho, " +
                    "comanda_detalhe, " +
                    "evento, " +
                    "fechamento_caixa, " +
                    "funcionario, " +
                    "\"many_venda_cabecalho_has_many_tipo_recebimento\", " +
                    "movimento_caixa, " +
                    "produto, " +
                    "sangria, " +
                    "suprimento, " +
                    "tipo_recebimento, " +
                    "unidade, " +
                    "venda_cabecalho, " +
                    "venda_detalhe RESTART IDENTITY");
            query.executeUpdate();           
                       
            s.getTransaction().commit();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "Erro ao limpar a base DAO: "+e.getMessage());
            s.getTransaction().rollback();
        } finally {
            s.close();
        }              
                
    }

}
